
package combat.dudes.client.gui.threads.shell;

import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.client.gui.ClientGUIInternalWindow;


/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class GUIShellMenu extends JMenuBar {

	private static final long serialVersionUID = 1L;
	private static int i = 0;

	private static JMenuItem addMenuItem(String menuTitle, Image icon, int keyEvent, int actionEvent, ActionListener doThis) {
		JMenuItem menuItem = (icon != null) ? new JMenuItem(menuTitle, new ImageIcon(icon)) : new JMenuItem(menuTitle); // Set title
		menuItem.setMargin(new Insets(3, 0, 1, 0));
		menuItem.setMnemonic(keyEvent); // Set underlined letter for hotkey
		menuItem.setAccelerator(KeyStroke.getKeyStroke(keyEvent, actionEvent)); // Add hotkey
		if(doThis != null) menuItem.addActionListener(doThis); // If set, perform action
		return menuItem;
	}
	/* final JMenuItem light = addMenuItem("Light", ((ClientGUITheme.currentTheme == ClientGUITheme.GUILaF.LIGHT) ? DudesFunctions.File.getIcon("tick.png") : null), KeyEvent.VK_L,
	 * ActionEvent.ALT_MASK,
	 * (ActionListener) new ActionListener() {
	 * public void actionPerformed(ActionEvent event) {
	 * new Thread() {
	 * public synchronized void run() {
	 * ClientGUITheme.setGUILookAndFeel(ClientGUITheme.GUILaF.LIGHT);
	 * synchronized(ClientGUI.gui_shell_repaint) {
	 * ClientGUI.gui_shell_repaint.notify();
	 * }
	 * }
	 * }.start();
	 * // SwingUtilities.invokeLater(refresh);
	 * setTick(ClientGUITheme.GUILaF.LIGHT);
	 * }
	 * });
	 * 
	 * final JMenuItem normal = addMenuItem("Normal", ((ClientGUITheme.currentTheme == ClientGUITheme.GUILaF.NORMAL) ? DudesFunctions.File.getIcon("tick.png") : null), KeyEvent.VK_N,
	 * ActionEvent.ALT_MASK, (ActionListener) new ActionListener() {
	 * public void actionPerformed(ActionEvent event) {
	 * new Thread() {
	 * public synchronized void run() {
	 * ClientGUITheme.setGUILookAndFeel(ClientGUITheme.GUILaF.NORMAL);
	 * synchronized(ClientGUI.gui_shell_repaint) {
	 * ClientGUI.gui_shell_repaint.notify();
	 * }
	 * }
	 * }.start();
	 * setTick(ClientGUITheme.GUILaF.NORMAL);
	 * }
	 * });
	 * 
	 * final JMenuItem dark = addMenuItem("Dark", ((ClientGUITheme.currentTheme == ClientGUITheme.GUILaF.DARK) ? DudesFunctions.File.getIcon("tick.png") : null), KeyEvent.VK_D, ActionEvent.ALT_MASK,
	 * (ActionListener) new ActionListener() {
	 * public void actionPerformed(ActionEvent event) {
	 * new Thread() {
	 * public synchronized void run() {
	 * ClientGUITheme.setGUILookAndFeel(ClientGUITheme.GUILaF.DARK);
	 * synchronized(ClientGUI.gui_shell_repaint) {
	 * ClientGUI.gui_shell_repaint.notify();
	 * }
	 * }
	 * }.start();
	 * setTick(ClientGUITheme.GUILaF.DARK);
	 * }
	 * }); */


	// -----------------------------------------------------

	public GUIShellMenu() {
		/* File
		 * > Disconnect
		 * > Quit
		 * 
		 * View
		 * > Restore Views (Alt+0)
		 * > =====================
		 * > World View (Alt+1)
		 * > Minimap (Alt+2)
		 * > Chat (Alt+3)
		 * > =====================
		 * > Theme's
		 * >light
		 * >medium
		 * >dark */

		// JMenuBar menuBar = new JMenuBar();
		// --> http://www.java-forums.org/advanced-java/30949-adding-menu-seperate-class-jframe.html

		// File menu

		JMenu menuFile = new JMenu("File");
		menuFile.setMnemonic(KeyEvent.VK_F);
		this.add(menuFile);

		// > Disconnect
		menuFile.add(addMenuItem("Disconnect", null, KeyEvent.VK_D, ActionEvent.ALT_MASK, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				ClientApp.SharedVars.logOut();
			}
		}));

		// > Quit
		menuFile.add(addMenuItem("Quit", null, KeyEvent.VK_Q, ActionEvent.ALT_MASK, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		}));


		// View menu
		JMenu menuView = new JMenu("View");
		menuView.setMnemonic(KeyEvent.VK_V);
		this.add(menuView);

		// > Restore Views
		menuView.add(addMenuItem("Restore Views", DudesFunctions.File.getIcon("monitor.png"), KeyEvent.VK_0, ActionEvent.ALT_MASK, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				for(ClientGUIInternalWindow window : GUIShell.windows)
					window.restoreWindow();
			}
		}));

		// =====================
		menuView.add(new JSeparator()); // SEPARATOR
		menuView.add(new JSeparator()); // SEPARATOR

		// Menu items (loop through all windows)
		// Hotkeys applied to the first 9 windows
		int[] hotkeys = { KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8, KeyEvent.VK_9 };
		for(final ClientGUIInternalWindow window : GUIShell.windows) {
			JMenuItem menuViewItem = new JMenuItem(window.getTitle(), window.getFrameIcon());
			menuViewItem.setMargin(new Insets(3, 0, 2, 0));
			if(window.id < 10) {
				menuViewItem.setMnemonic(hotkeys[window.id]);
				menuViewItem.setAccelerator(KeyStroke.getKeyStroke(hotkeys[window.id], ActionEvent.ALT_MASK));
			}
			menuViewItem.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					window.restoreWindow();
				}
			});
			menuView.add(menuViewItem);
		}

		/* menuView.add(new JSeparator()); // SEPARATOR
		 * menuView.add(new JSeparator()); // SEPARATOR
		 * 
		 * menuView.add(addMenuItem("Themes", null, KeyEvent.VK_T, ActionEvent.ALT_MASK, (ActionListener) new ActionListener() {
		 * public void actionPerformed(ActionEvent event) {
		 * // ...
		 * }
		 * }));
		 * JMenu menuThemes = new JMenu("Themes");
		 * menuThemes.setMnemonic(KeyEvent.VK_F);
		 * this.add(menuThemes);
		 * 
		 * menuView.add(menuThemes);
		 * 
		 * menuThemes.add(light);
		 * menuThemes.add(normal);
		 * menuThemes.add(dark);
		 * 
		 * 
		 * }
		 * public void setTick(ClientGUITheme.GUILaF theme) {
		 * if(theme == ClientGUITheme.GUILaF.LIGHT)
		 * light.setIcon(new ImageIcon(DudesFunctions.File.getIcon("tick.png")));
		 * else light.setIcon(null);
		 * if(theme == ClientGUITheme.GUILaF.NORMAL)
		 * normal.setIcon(new ImageIcon(DudesFunctions.File.getIcon("tick.png")));
		 * else normal.setIcon(null);
		 * if(theme == ClientGUITheme.GUILaF.DARK)
		 * dark.setIcon(new ImageIcon(DudesFunctions.File.getIcon("tick.png")));
		 * else dark.setIcon(null);
		 * } */

		this.add(Box.createHorizontalGlue());
		this.add(menuTime);
	}
	public static JMenu menuTime = new JMenu();

}
